package org.morningangel.dao.impl;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import javax.naming.NamingException;

import org.morningangel.bean.Activity;
import org.morningangel.bean.Status;
import org.morningangel.bean.User;
import org.morningangel.dao.StatusDao;
import org.morningangel.util.DBHelperUtil;

public class StatusDaoImpl implements StatusDao {

	@Override
	public Integer save(Status status) {
		Connection conn = null;
		PreparedStatement ps = null;
		Integer suNum = 0;
		try {
			conn = DBHelperUtil.getConnection();
			// 开启事务
			conn.setAutoCommit(false);
			String sql = "insert into tb_status (userId, activityId, status) values (?, ?, ?)";
			ps = conn.prepareStatement(sql);
			ps.setInt(1, status.getUserId());
			ps.setInt(2, status.getActivityId());
			ps.setInt(3, status.getStatus());
			ps.executeUpdate();
			//
			ps.close();
			if (status.getStatus() == 1) {
				sql = "update tb_activity set interestNum = interestNum + 1 where id = ?";
			} else {
				sql = "update tb_activity set partipateNum = partipateNum + 1 where id = ?";
			}
			ps = conn.prepareStatement(sql);
			ps.setInt(1, status.getActivityId());
			suNum = ps.executeUpdate();
			// 提交事务
			conn.commit();
			// 关闭事务
			conn.setAutoCommit(true);
		} catch (NamingException e) {
			e.printStackTrace();
		} catch (SQLException e) {
			e.printStackTrace();
		}

		DBHelperUtil.close(ps, conn);

		return suNum;
	}

	@Override
	public Status get(Integer userId, Integer activityId) {
		Connection conn = null;
		PreparedStatement ps = null;
		ResultSet rs = null;
		Status status = null;
		try {
			conn = DBHelperUtil.getConnection();
			String sql = "select id, userId, activityId, status from tb_status where userId = ? and activityId = ?";
			ps = conn.prepareStatement(sql);
			ps.setInt(1, userId);
			ps.setInt(2, activityId);
			rs = ps.executeQuery();
			if (rs.next()) {
				status = new Status();
				setStatus(rs, status);
			}
		} catch (NamingException e) {
			e.printStackTrace();
		} catch (SQLException e) {
			e.printStackTrace();
		}

		DBHelperUtil.close(rs, ps, conn);

		return status;
	}

	@Override
	public Integer update(Status old, Status status) {
		Connection conn = null;
		PreparedStatement ps = null;
		Integer suNum = 0;
		try {
			conn = DBHelperUtil.getConnection();
			// 打开事物
			conn.setAutoCommit(false);
			String sql = "update tb_status set status = ? where id = ?";
			ps = conn.prepareStatement(sql);
			ps.setInt(1, status.getStatus());
			ps.setInt(2, old.getId());
			ps.executeUpdate();
			//
			ps.close();
			Integer st = status.getStatus() - old.getStatus();
			if (st == 1) {
				// 添加感兴趣人数
				sql = "update tb_activity set interestNum = interestNum + 1 where id = ?";
			} else {
				// 2,添加参加人数
				sql = "update tb_activity set partipateNum = partipateNum + 1 where id = ?";
			}
			ps = conn.prepareStatement(sql);
			ps.setInt(1, old.getActivityId());
			suNum = ps.executeUpdate();
			// 提交事务
			conn.commit();
			// 关闭事务
			conn.setAutoCommit(true);
		} catch (NamingException e) {
			e.printStackTrace();
		} catch (SQLException e) {
			e.printStackTrace();
		}

		DBHelperUtil.close(ps, conn);

		return suNum;
	}

	@Override
	public List<Activity> getActivities(Integer userId, Integer status) {
		Connection conn = null;
		PreparedStatement ps = null;
		ResultSet rs = null;
		List<Activity> activities = new ArrayList<Activity>();
		try {
			conn = DBHelperUtil.getConnection();
			String sql = "select a.id, a.title, a.mainImage, a.image, a.startTime, a.endTime, a.type, a.address, a.sponsor, a.description, a.url, a.status, a.publishTime, a.partipateNum, a.interestNum from tb_activity a, tb_status s where a.id = s.activityId and s.userId = ? and (s.status = ? or s.status = 3)";
			ps =conn.prepareStatement(sql);
			ps.setInt(1, userId);
			ps.setInt(2, status);
			rs = ps.executeQuery();
			while (rs.next()) {
				Activity activity = new Activity();
				setActivity(rs, activity);
				activities.add(activity);
			}
		} catch (NamingException e) {
			e.printStackTrace();
		} catch (SQLException e) {
			e.printStackTrace();
		}
		
		DBHelperUtil.close(rs, ps, conn);
		
		return activities;
	}

	@Override
	public List<User> getUsers(Integer activityId, Integer status,
			Integer pageNum, Integer pageSize) {

		return null;
	}

	private void setStatus(ResultSet rs, Status status) throws SQLException {
		status.setId(rs.getInt(1));
		status.setUserId(rs.getInt(2));
		status.setActivityId(rs.getInt(3));
		status.setStatus(rs.getInt(4));
	}
	
	private void setActivity(ResultSet rs, Activity activity) throws SQLException {
		activity.setId(rs.getInt(1));
		activity.setTitle(rs.getString(2));
		activity.setMainImage(rs.getString(3));
		activity.setImage(rs.getString(4));
		activity.setStartTime(new Date(rs.getTimestamp(5).getTime()));
		activity.setEndTime(new Date(rs.getTimestamp(6).getTime()));
		activity.setType(rs.getString(7));
		activity.setAddress(rs.getString(8));
		activity.setSponsor(rs.getString(9));
		activity.setDescription(rs.getString(10));
		activity.setUrl(rs.getString(11));
		activity.setStatus(rs.getInt(12));
		activity.setPublishTime(new Date(rs.getTimestamp(13).getTime()));
		activity.setPartipateNum(rs.getInt(14));
		activity.setInterestNum(rs.getInt(15));
	}

}
